<?php

class Admin_EntryController extends Zend_Controller_Action {

    public function init() {
        $this->view->headTitle('zendblog')->headTitle('admin')->setSeparator(' :: ');
        $this->view->title = 'admin';
    }

    public function addAction() {
        $entryForm = new Application_Form_EntryAdd();

        $values = array(
            'note' => ZendBlog_Filter_HtmlEntry::getLabel()
        );

        if (!$this->getRequest()->isPost()) {
            $entryForm->populate($values);
            $this->view->entryForm = $entryForm;
            return;
        } elseif (!$entryForm->isValid($_POST)) {
            $entryForm->populate($values);
            $this->view->isInvalid = true;
            $this->view->entryForm = $entryForm;
            return;
        }

        $tableEntries = new Application_Model_DbTable_Entries();
        $data = array(
            'title' => $entryForm->getValue('title'),
            'date' => $entryForm->getValue('date'),
            'author' => Zend_Auth::getInstance()->getIdentity()->username,
            'user_id' => Zend_Auth::getInstance()->getIdentity()->id,
            'body' => $entryForm->getValue('body'),
        );
        $tableEntries->insert($data);
        $this->view->isSaved = true;
    }

    public function listAction() {
        $tableEntries = new Application_Model_DbTable_Entries();
        $query;
        if (Zend_Auth::getInstance()->getIdentity()->admin) {
            $query = $tableEntries->select()
                            ->from($tableEntries, array('title', 'date', 'id'))
                            ->order('date desc');
        } else {
            $query = $tableEntries->select()
                            ->from($tableEntries, array('title', 'date', 'id'))
                            ->where('user_id = ?', Zend_Auth::getInstance()->getIdentity()->id)
                            ->order('date desc');
        }

        $adapter = new Zend_Paginator_Adapter_DbSelect($query);
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber($this->_getParam('page', 1));
        $this->view->paginator = $paginator;
    }

    public function editAction() {
        $entryForm = new Application_Form_EntryEdit;
        if (!$this->getRequest()->isPost()) {
            $tableEntries = new Application_Model_DbTable_Entries();
            $query;
            if (Zend_Auth::getInstance()->getIdentity()->admin) {
                $query = $tableEntries->select()
                                ->where('id = ?', $this->_getParam('id'));
            } else {
                $query = $tableEntries->select()
                                ->where('id = ?', $this->_getParam('id'))
                                ->where('user_id = ?', Zend_Auth::getInstance()->getIdentity()->id);
            }
            $rowset = $tableEntries->fetchAll($query);
            if (count($rowset) == 0) {
                $this->view->failedFind = true;
                return;
            }
            $entryForm->setElementFilters(array()); // disable all element filters
            $this->_repopulateForm($entryForm, $rowset->current());
            $this->view->entryForm = $entryForm;
            return;
        } elseif (!$entryForm->isValid($_POST)) {
            $this->view->isInvalid = true;
            $this->view->entryForm = $entryForm;
            return;
        }

        $tableEntries = new Application_Model_DbTable_Entries();
        $data = array(
            'title' => $entryForm->getValue('title'),
            'date' => $entryForm->getValue('date'),
            'body' => $entryForm->getValue('body')
        );
        $tableEntries->update($data,
                $tableEntries->getAdapter()->quoteInto('id = ?', $entryForm->getValue('id'))
        );
        $this->view->isSaved = true;
    }

    public function deleteAction() {
        if (!$this->getRequest()->isPost()) {
            $tableComment = new Application_Model_DbTable_Comments();
            $tableEntries = new Application_Model_DbTable_Entries();
            $query;
            if (Zend_Auth::getInstance()->getIdentity()->admin) {
                $query = $tableEntries->select()
                                ->where('id = ?', $this->_getParam('id'));
            } else {
                $query = $tableEntries->select()
                                ->where('id = ?', $this->_getParam('id'))
                                ->where('user_id = ?', Zend_Auth::getInstance()->getIdentity()->id);
            }

            $rowset = $tableEntries->fetchRow($query);
            if (count($rowset) == 0) {
                $this->view->failedFind = true;
                return;
            } else {
                $tableComment->delete($tableComment->getAdapter()->quoteInto('entry_id = ?', $this->_getParam('id')));
                $tableEntries->delete($tableEntries->getAdapter()->quoteInto('id = ?', $this->_getParam('id')));
            }
        }
    }

    protected function _repopulateForm($form, $entry) {
        $values = array(
            'title' => $this->_reverseAutoFormat($entry->title),
            'date' => $entry->date,
            'body' => $this->_reverseAutoFormat($entry->body),
            'id' => $this->_getParam('id'),
            'note' => ZendBlog_Filter_HtmlEntry::getLabel()
        );
        $form->populate($values);
    }

    protected function _reverseAutoFormat($string) {
        $string = preg_replace("/\<p\>/", "", $string);
        $string = preg_replace("/\<\/p\>/", "\n\n", $string);
        $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
        return $string;
    }

}

